﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace KingSite.Library.Compress {
    public static class CompressUtility {
        // Methods
        public static byte[] Compress(byte[] data, CompressType type) {
            byte[] buffer;
            using (MemoryStream stream = new MemoryStream(data)) {
                using (MemoryStream stream2 = new MemoryStream()) {
                    Compress(stream, stream2, type);
                    buffer = stream2.ToArray();
                }
            }
            return buffer;
        }

        public static byte[] Compress(Stream stream, CompressType type) {
            using (MemoryStream stream2 = new MemoryStream()) {
                Compress(stream, stream2, type);
                return stream2.ToArray();
            }
        }


        public static void Compress(Stream inStream, Stream outStream, CompressType type) {
            Stream output = (type == CompressType.Deflate) ? ((Stream)new DeflateStream(outStream, CompressionMode.Compress)) : ((Stream)new GZipStream(outStream, CompressionMode.Compress));
            using (BinaryReader reader = new BinaryReader(inStream)) {
                using (BinaryWriter writer = new BinaryWriter(output)) {
                    byte[] buffer;
                    while ((buffer = reader.ReadBytes(0x400)).Length > 0) {
                        writer.Write(buffer);
                    }
                    writer.Flush();
                }
            }
        }

        public static void Compress(string filename, string compressFilename, CompressType type) {
            using (FileStream stream = new FileStream(filename, FileMode.Open)) {
                using (FileStream stream2 = new FileStream(compressFilename, FileMode.Create)) {
                    Compress(stream, stream2, type);
                }
            }
        }

        public static byte[] Decompress(byte[] data, CompressType type) {
            byte[] buffer;
            using (MemoryStream stream = new MemoryStream(data)) {
                using (MemoryStream stream2 = new MemoryStream()) {
                    Decompress(stream, stream2, type);
                    buffer = stream2.ToArray();
                }
            }
            return buffer;
        }

        public static byte[] Decompress(Stream stream, CompressType type) {
            using (MemoryStream stream2 = new MemoryStream()) {
                Decompress(stream, stream2, type);
                return stream2.ToArray();
            }
        }

        public static void Decompress(Stream inStream, Stream outStream, CompressType type) {
            Stream input = (type == CompressType.Deflate) ? ((Stream)new DeflateStream(inStream, CompressionMode.Decompress)) : ((Stream)new GZipStream(inStream, CompressionMode.Decompress));
            using (BinaryReader reader = new BinaryReader(input)) {
                using (BinaryWriter writer = new BinaryWriter(outStream)) {
                    byte[] buffer;
                    while ((buffer = reader.ReadBytes(0x400)).Length > 0) {
                        writer.Write(buffer);
                    }
                    writer.Flush();
                }
            }
        }

        public static void Decompress(string compressFilename, string filename, CompressType type) {
            using (FileStream stream = new FileStream(compressFilename, FileMode.Open)) {
                using (FileStream stream2 = new FileStream(filename, FileMode.Create)) {
                    Decompress(stream, stream2, type);
                }
            }
        }

    }

    public enum CompressType {
        Deflate,
        GZip
    }


}
